﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using UnityEngine.Terrain;

namespace UnityEngine.Graphics
{
    public class VertexInfo
    {
        public VertexInfo(WCLoader loader)
        {
            GroundHeight = Position.Y = loader.ReadInt8() + 256 * loader.ReadInt8();
            WaterLevel = loader.ReadInt8() + 256 * loader.ReadInt8();

            byte word = (Byte)loader.ReadByte();
            Flags = (Byte)((word & 0xf0) >> 4);
            TextureId = (Byte)(word & 0x0f);

            TextureDetail = loader.ReadByte();

            word = loader.ReadByte();
            CliffTextureType = (Byte)((word & 0xf0) >> 4);
            LayerHeight = (Byte)(word & 0x0f);
        }

        public float GroundHeight;
        public Vector3 Position; // 2 bytes
        public int WaterLevel;   // 2 bytes
        public byte Flags; // 0.5 byte
        public byte TextureId; // 0.5 byte
        public byte TextureDetail; // 1 byte
        public byte CliffTextureType; // 0.5 byte
        public byte LayerHeight; // 0.5 byte

        // Data 
        private static int _DebugVertexInfoId = 0;

        private int _DebugId;

        private bool[] _SurfaceVisible = { true, true, true, true };  // A surface is invisible if four points are all invisible

        public bool[] SurfaceVisible
        {
            get { return _SurfaceVisible; }
            set { _SurfaceVisible = value; }
        }

        public bool IsRampPoint
        {
            get { return Convert.ToBoolean(Flags & 1); }
        }

        private VertexInfo()
        {
            _DebugId = _DebugVertexInfoId;
            _DebugVertexInfoId++;
        }

        private int _PrintCount = 0;
        public void PrintDebugInfo()
        {
            Debug.Write(" Count " + _PrintCount + "   ");
            Debug.Write(" X " + _DebugId % 129 + "   ");
            Debug.Write(" Y " + _DebugId / 129 + "   ");
            Debug.Write(" ID " + _DebugId);
            Debug.Write(" Surface Visible " + _SurfaceVisible);

            Debug.WriteLine("");
        }

        public void DebugPrint()
        {
            Debug.WriteLine("Vertex Info");
            Debug.WriteLine(string.Format("{0:x4}", Flags));
            Debug.WriteLine("Texture Id " + TextureId);
            Debug.WriteLine("Texture Detail " + TextureDetail);
        }
    }
}
